from QQLoginTool.QQtool import OAuthQQ
from django.conf import settings
from django.shortcuts import render

# Create your views here.
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from oauth.models import OAuthQQUser

from oauth.utils import generate_sign_openid
from oauth import serializers


class QQLoginView(APIView):
    def get(self,request):
        next = request.query_params.get("state","/")
        oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET,
                        redirect_uri=settings.QQ_REDIRECT_URI, state=next)
        login_url = oauth.get_qq_url()
        return Response({"auth_url":login_url})

        pass
    pass

class BindQQLoginView(GenericAPIView):
    serializer_class = serializers.UersSerializer

    def get(self,request):
        code = request.query_params.get("code")
        if not code:
            return Response("code丢失了")
        oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET,
                        redirect_uri=settings.QQ_REDIRECT_URI, state="/")
        access_token = oauth.get_access_token(code)
        openid = oauth.get_open_id(access_token)
        #根据openid获取用户,不一定能获取到
        try:
            oauth_user = OAuthQQUser.objects.get(openid=openid)
        except OAuthQQUser.DoesNotExist:
            #初次授权,需要绑定
            access_token = generate_sign_openid(openid)
            return Response({"access_token":access_token})
            pass
        else:
            user = oauth_user.user

            from rest_framework_jwt.settings import api_settings
            jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
            jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)
            user.token = token

            return Response({
                "token":token,
                "username":user.username,
                "user_id":user.id
            })
        pass

    def post(self,request):
        #获取参数
        # password: this.password,
        # mobile: this.mobile,
        # sms_code: this.sms_code,
        # access_token: this.access_token
        dict_data = request.data

        #创建序列器对象,校验入库
        serializer = self.get_serializer(data = dict_data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()

        #生成token
        from rest_framework_jwt.settings import api_settings
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)
        user.token = token

        #返回响应
        return Response({
            "token":token,
            "user_id":user.id,
            "username":user.username,
        })
        pass
    pass



